const errMsg=['请输入验证邮箱','邮箱格式不正确','请输入验证码',
    '验证码不正确','请输入新密码','密码不能少于8位',
    '请再次确认新密码','两次密码不一致','网络不稳定,请稍后重试'];
const okMsg=['验证完成,请下一步完成密码找回'];
var $emailForm=$('.findpass-form');
var $safeForm=$('.safe-form');
var $okForm=$('.ud-ok');
var $emailInput=$('.check-email input');
var $inputs=$('.form-wrap input');
var $codeInput=$('.hover-vcode label input');
var $hoverCheck=$('.hover-vcode');
var $checkBtn=$('.check-code');
var $codeBtn=$('.hover-vcode .btn');
var $udBtn=$('.sure');
var $tips=$('.tip');
var $next=$('.next');
let isCheckOK=false;
let checkCode='';
$inputs.focus(function () {
    $(this).parent().css('border-color','#666');
});
$inputs.blur(function () {
    $(this).parent().css('border-color','#ddd');
});
$inputs.keydown(function () {
    let index=$(this).index('.form-wrap input');
    $tips[index].innerHTML='';
});
let isSend=false;
$checkBtn.click(async function () {
    let email=$inputs[0].value;
    if(!email.length){
        $emailInput.focus();
        $tips[0].innerHTML=`<i></i><span class="err">${errMsg[0]}</span>`;
    }else if(!/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(email)){
        $emailInput.focus();
        $tips[0].innerHTML=`<i></i><span class="err">${errMsg[1]}</span>`;
    }else{
        if(!isSend){
            $hoverCheck.show();
            isSend=true;
            checkCode=await new Promise((resolve)=>{
                $.ajax({
                    type:'POST',
                    data:{check:{type:'email',data:email,sendOnly:true}},
                    url:'http://127.0.0.1:8080/check',
                    success:(data)=>{
                        resolve(data.result)
                    }
                });
            });
        }
    }
});
$codeBtn.click(function () {
    let code=$inputs[1].value;
    if(!code.length){
        $codeInput.focus();
        $tips[1].innerHTML=`<i></i><span class="err">${errMsg[2]}</span>`;
    }else if(code!==checkCode){
        $tips[1].innerHTML=`<i></i><span class="err">${errMsg[3]}</span>`;
    }else{
        isCheckOK=true;
        $codeInput.prop('readonly','readonly');
        $tips[1].innerHTML=`<i class="ok"></i><span class="ok">${okMsg[0]}</span>`;
    }

    if(isCheckOK){
        $next.css({
            opacity:1,
            cursor:'pointer'
        });
    }
});
$next.click(function () {
    if(isCheckOK){
        $emailForm.hide();
        $safeForm.show();
    }
});

$udBtn.click(async function () {
    let email=$inputs[0].value;
    let pwd=$inputs[2].value;
    let pwd2=$inputs[3].value;
    if(!pwd.length){
        $tips[2].innerHTML=`<i></i><span class="err">${errMsg[4]}</span>`;
    }else if(pwd.length<8){
        $tips[2].innerHTML=`<i></i><span class="err">${errMsg[5]}</span>`;
    }else if(!pwd2.length){
        $tips[3].innerHTML=`<i></i><span class="err">${errMsg[6]}</span>`;
    }else if(pwd!==pwd2){
        $tips[3].innerHTML=`<i></i><span class="err">${errMsg[7]}</span>`;
    }else{
        $udBtn.text('正在修改...');
        let UdStatus=await new Promise((resolve)=>{
            setTimeout(()=>{
                $.ajax({
                    type:'POST',
                    data:{email,pwd},
                    url:'http://127.0.0.1:8080/findpass',
                    success:(data)=>{
                        resolve(data)
                    }
                });
            },500)
        });
        if(UdStatus==1){
            $safeForm.hide();
            $okForm.show();
        }else{
            $udBtn.text('确认修改');
            $tips[4].innerHTML=`<i></i><span class="err">${errMsg[8]}</span>`;
        }
    }
});